<template>
	<view>
		<topbar title="发布动态" background="background:#7ADEEC"></topbar>
		<view class="content">
			<view class="dis_f_co top">
				<view class="title">动态内容</view>
				<view style="border-bottom: 2rpx solid #EEE;">
					<u-input type="textarea" v-model="text" auto-height height="70" maxlength="9999"
						placeholder="请输入信息描述"></u-input>
				</view>
			</view>
			<view class="top" style="margin-bottom: 36rpx;">
				<view class="dis_f_sb_c" style="margin-bottom: 20rpx;">
					<view>动态类型</view>
					<u-radio-group v-model="value" @change="radioGroupChange" size="28">
						<u-radio name="1" label-size="28">图文动态</u-radio>
						<u-radio name="2" label-size="28">视频动态</u-radio>
					</u-radio-group>
				</view>
				<view v-if="type ==1">
					<view class=" dis_f_sb_c" style="margin-bottom: 24rpx;">
						<view class="dis_f_c_c title" style="margin: 0;">上传图片</view>
						<view class="dis_f_c_c size_22">最多上传6张图片</view>
					</view>
					<view style="width: 100%;">
						<u-upload ref="uUpload" :action="action" :file-list="fileList" width="140" height="140"
							max-count="6"></u-upload>
					</view>
				</view>
				<view v-if="type ==2">
					<view class=" dis_f_sb_c" style="margin-bottom: 24rpx;">
						<view class="dis_f_c_c title" style="margin: 0;">上传视频</view>
						<!-- <view class="dis_f_c_c size_22">最多上传6张图片</view> -->
					</view>
					<view class="dis_f_co" >
						
						<htz-image-upload  :max="1" v-model="ceshiData"
							@uploadSuccess="zidingyiSuccess" action="http://jk85472adeg.htjy668.cn/index/index/upload" mediaType="video"></htz-image-upload>

					</view>
				</view>
			</view>
			<view class="dz_btn dis_f_c_c">
				<image class="left" src="../../static/icon/dw2.png" mode=""></image>
				<view style="margin-right: 18rpx;">{{city}}</view>
				<!-- <image class="right" src="../../static/icon/delet.png" mode=""></image> -->
			</view>
			<view style="position: fixed;bottom: 84rpx;left: 24rpx;" >
				<view class="btn btn1 dis_f_c_c" @click="go_part2" v-if="yes">
					发布动态
				</view>
				<view class="btn btn2 dis_f_c_c" @click="fail" v-else>
					发布动态
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import htzImageUpload from '@/components/htz-image-upload/htz-image-upload.vue'

	export default {
		components: {
			htzImageUpload,
		},
		data() {
			return {
				action: 'http://jk85472adeg.htjy668.cn/index/index/upload',
				fileList: [],
				text: '',
				yes: false,
				city: '',
				red: 0,
				city: '',
				lat: '',
				lng: '',
				value: 1,
				type: 1,
				video: '',
				ceshiData: [],
			};
		},
		watch: {
			text(newValue, oldValue) {
				if (this.text != '') {
					this.yes = true
				} else {
					this.yes = false
				}
			}
		},
		onLoad(e) {
			this.get_location()
		},
		methods: {
			
			zidingyiSuccess(res) {
				var _res = JSON.parse(res.data);
				console.log(_res)
				if(_res.code == 1){
					let src = 'http://jk85472adeg.htjy668.cn/' + _res.src
					this.ceshiData.push(src);
					this.video = _res.id
					console.log(src)
				}else{
					this.$.ti_shi('上传失败')
				}
				

			},
			radioGroupChange(e) {
				this.type = e
				console.log(this.type)
			},
			get_location() {
				let that = this
				console.log(12121)
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: function(res) {
						console.log(res)
						that.city = res.address.city
						that.lat = res.latitude
						that.lng = res.longitude
					},
					fail: function(res) {
						console.log(res)
					}
				});
			},
			go_part2() {
				this.yes = false;
				if(this.type ==1){
					this.$.ajax(1, 'post', 'index/checktokens', {
						uid: uni.getStorageSync('uid'),
						token: uni.getStorageSync('token')
					}, (res) => {
						console.log(res)
						if (res.code == 9) {
							this.$.ti_shi(res.msg)
							uni.clearStorageSync()
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}, 1000)
							return;
						} else {
							let files = [];
							// 通过filter，筛选出上传进度为100的文件(因为某些上传失败的文件，进度值不为100，这个是可选的操作)
							// files = this.$refs.uUpload.lists.filter(val => {
							// 	return val.progress == 100;
							// })
							// 如果您不需要进行太多的处理，直接如下即可
							files = this.$refs.uUpload.lists;
							if (files == '') {
								this.$.ti_shi('请上传图片')
								return;
							} else {
								files.forEach((item, key) => {
									if (item.response != null) {
										this.fileList[key] = item.response.id
									}
								})
							}
							// 　　
							this.$.ajax(1, 'post', 'index/setquan', {
								uid: uni.getStorageSync('uid'),
								token: uni.getStorageSync('token'),
								content: this.text,
								pic: this.fileList.join(','),
								city: this.city,
								lat: this.lat,
								lng: this.lng,
								type:this.type
							}, (res) => {
								console.log(res)
								this.yes = true;
								if (res.code == 1) {
									this.$.ti_shi(res.msg)
									setTimeout(() => {
										uni.navigateTo({
											url: '/pages/guang_chang/dt_gl'
										})
									}, 1000)
								} else {
									this.$.ti_shi(res.msg)
								}
					
							})
						}
					})
					
					console.log(this.fileList)
				}else{
					this.$.ajax(1, 'post', 'index/checktokens', {
						uid: uni.getStorageSync('uid'),
						token: uni.getStorageSync('token')
					}, (res) => {
						console.log(res)
						if (res.code == 9) {
							this.$.ti_shi(res.msg)
							uni.clearStorageSync()
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}, 1000)
							return;
						} else {
							// files = this.$refs.uUpload.lists;
							if (this.ceshiData == '') {
								this.$.ti_shi('请上传视频')
								return;
							} else {
								this.$.ajax(1, 'post', 'index/setquan', {
									uid: uni.getStorageSync('uid'),
									token: uni.getStorageSync('token'),
									content: this.text,
									pic: this.video,
									city: this.city,
									lat: this.lat,
									lng: this.lng,
									type:this.type
								}, (res) => {
									console.log(res)
									if (res.code == 1) {
										this.$.ti_shi(res.msg)
										setTimeout(() => {
											uni.navigateTo({
												url: '/pages/guang_chang/dt_gl'
											})
										}, 1000)
									} else {
										this.$.ti_shi(res.msg)
									}
													
								})
							}
						}
					})
					
					console.log(this.fileList)
				}
			},
			fail() {
				this.$.ti_shi('请完善信息')
			}
		}
	}
</script>

<style lang="scss">
	.content {
		width: 100%;
		top: calc(var(--status-bar-height) + 88rpx);
		z-index: 998;
		position: absolute;
		padding: 36rpx 24rpx;

		.dz_btn {
			width: 166rpx;
			height: 56rpx;
			background-color: #fff;
			border-radius: 30rpx;
			font-size: 22rpx;
			line-height: 32rpx;
			color: #7ADEEC;

			.left {
				width: 18.45rpx;
				height: 25.5rpx;
				margin-right: 12rpx;
			}

			.right {
				width: 15.95rpx;
				height: 15.95rpx;
			}
		}

		.top {
			width: 100%;
			margin-bottom: 12rpx;
			padding: 36rpx 24rpx;
			background-color: #fff;
			border-radius: 20rpx;
			box-shadow: 0rpx 6rpx 24rpx rgba(34, 34, 34, 0.05);

			.title {
				font-size: 28rpx;
				line-height: 40rpx;
				color: #222222;
				margin-bottom: 24rpx;
			}
		}

		.btn {
			width: 702rpx;
			height: 98rpx;

			color: #FFFFFF;
			border-radius: 50rpx;
		}

		.btn1 {
			background-color: #E23C63;
		}

		.btn2 {
			background-color: #bbb;
		}
	}
</style>
